package com.jld.managerialsystemforcovid19.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.jld.managerialsystemforcovid19.entity.User;
import com.jld.managerialsystemforcovid19.service.UserService;
import com.jld.managerialsystemforcovid19.vo.DataView;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 贾老丁
 * @description 用户登录
 * @Date 2022/11/24 16:28
 */
@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    //验证码登录逻辑
    @RequestMapping("/login/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        //1.验证码对象HuTool定义图形验证码的长和宽,验证码的位数，干扰线的条数
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 10);
        //2.放入到session
        session.setAttribute("code", lineCaptcha.getCode());
        try {
            //3.输出验证码
            ServletOutputStream outputStream = response.getOutputStream();
            lineCaptcha.write(outputStream);
            //4.关闭输出流
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //具体的登录逻辑
    @RequestMapping("/login/login")
    @ResponseBody
    public DataView login(String username, String password, String code, HttpSession session) {
        DataView dataView = new DataView();
        //1.先判断验证码是否正确
        String sessionCode = (String) session.getAttribute("code");
        if (code != null && sessionCode.equals(code)) {
            //2.如果验证码正确，用户登录(session登录)
            User user = userService.login(username, password);
            //shiro登录
//            Subject subject = SecurityUtils.getSubject();
//            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//            subject.login(token);
//            User user = (User) subject.getPrincipal();
            //3.判断用户是否为空
            if (user != null) {
                dataView.setCode(200);
                //4.放入session
                session.setAttribute("user", user);
                dataView.setMsg("登陆成功！");
                return dataView;
            } else {
                dataView.setCode(100);
                dataView.setMsg("用户名或密码错误，请重新输入！");
            }
        }
        dataView.setCode(100);
        dataView.setMsg("验证码错误！");
        return dataView;
    }

    //退出登录
    @RequestMapping("/login/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("username");
        return "login";
    }

}